﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Oxite.ViewModels;
using Oxite.Validation;
using ECube.Web.CMS;
using ECube.Web.CMS.Extensions;
using ECube.Web.CMS.Models;
using ECube.Web.CMS.Services;
namespace ECube.Web.CMS.Controllers
{
    public class SiteNavigatorController : AsyncController
    {
        //IPageService pageService;
        //public SiteNavigatorController(IPageService pageService)
        //{
        //    this.pageService = pageService;
        //}
        public MvcSiteMapNode Manage(String item)
        {
            if (!String.IsNullOrEmpty(item))
            {               
            }
            this.ViewData.Add("Title", "站点导航设置");
            return SiteNavigatorService.Current.RootNode;
        }

        public ActionResult Item(string key)
        {
            SiteMapNode node = SiteNavigatorService.Current.FindNode(key);
            if (node != null)
            {
                return this.PartialView("Item", node.ToInput());
            }
            return this.PartialView("Item",null);
        }
       

        [HttpPost]
        public JsonResult GetNode(String key)
        {
            if (String.IsNullOrEmpty(key))
            {
                return null;
            }
            SiteMapNode node = SiteNavigatorService.Current.FindNode(key);
            if (node != null)
            {
                return Json(node.ToInput());
            }
            return null;
        }
        [HttpPost]
        public  ActionResult AddNode(MvcSiteMapNodeInput input)
        {
            
            if (input.Status == MvcSiteMapNodeStatus.New)
            {
                this.ModelState.Remove("Status");
                this.ModelState.Clear();
                input.Status = MvcSiteMapNodeStatus.NewEditing;
                
                if(string.IsNullOrEmpty(input.Url)) input.Url = "~/";
            }           
            else if (ModelState.IsValid)
            {
                SiteNavigatorService service = SiteNavigatorService.Current;
                SiteMapNode node = service.NewNode(input);                
                if (node == null)
                {
                    //ModelState.AddModelError();
                }
                else if (service.AddNode(node, input.ParentKey))
                {
                    //this.ModelState.Clear();
                    this.ModelState.Remove("Status");
                    input.Status = MvcSiteMapNodeStatus.Changed;
                }
                else
                {
                    //ModelState.AddModelError();
                }
                
            }
            
            return this.PartialView("Item",input);
                             
        }

        public void UpdateNodePageId(string nodeKey, string pageid)
        {
            MvcSiteMapNode node = SiteNavigatorService.Current.FindNode(nodeKey) as MvcSiteMapNode;
            if (node != null)
            {
                node.PageId = pageid;
                SiteNavigatorService.Current.Save();
            }
        }

        [HttpPost]
        public ActionResult EditNode(MvcSiteMapNodeInput input)
        {
            this.ModelState.Remove("Status");
            input.Status = MvcSiteMapNodeStatus.Editing;
            if (ModelState.IsValid)
            {
                SiteMapNode node = SiteNavigatorService.Current.FindNode(input.Key);
                if (node == null)
                {
                    //ModelState.AddModelError();
                }
                else
                {
                    node.FromInput(input);
                    if (SiteNavigatorService.Current.Save())//保存                        
                    {
                        this.ModelState.Remove("Status");
                        input.Status = MvcSiteMapNodeStatus.Changed;
                       
                    }
                    else
                    {
                        //ModelState.AddModelError();
                    }
                }
            }
             return this.PartialView("Item", input);
           
        }

        [HttpPost]
        public JsonResult Remove(String key)
        {
            if (!String.IsNullOrEmpty(key))
            {
                if (SiteNavigatorService.Current.RemoveNode(key))
                {
                    SiteNavigatorService.Current.Save();
                    return Json(true);
                }
            }
            return Json(false);
        }

        [HttpPost]
        public JsonResult Move(String sourceKey, String targetKey, NodeMovePosition position)
        {
            bool re = SiteNavigatorService.Current.Move(sourceKey,targetKey, position);
            if (re)
            {
                SiteNavigatorService.Current.Save();
            }
            return Json(re);        
        }
        [HttpPost]
        public JsonResult Check(string field, string value)
        {
            SiteNavigatorService service = SiteNavigatorService.Current;
            string success = "true";
            string message = "";
            ValidationError error = null;
            switch (field)
            {
                case "key":
                    error = service.ValidateKey(value);                                    
                    break;
                case "url":
                    error = service.ValidateUrl(value);
                    break;
                default: 
                    break;
            }
            if(error != null)
            {
                success = "false";
                message = error.Message;
            }
            return Json(new { success = success, message = message });
        }

    }
}
